﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xenta.Enums;
using Xenta.Operations;
using Xenta.Utils;

namespace Xenta.Tests.Operations
{
    [TestFixture]
    public class PermissionTests : OperationTest
    {
        protected override IEnumerable<Type> ModelMappings
        {
            get
            {
                return NHMappings.ALL;
            }
        }

        [TestCase("xy", "name")]
        public void Can_CRUD(string code, string name)
        {
            var permission = Processor.Execute(new CreatePermission
            {
                Code = code,
                Name = name
            }).Result;

            Assert.NotNull(permission);
            permission.Code.AreEqualIgnoringCase(code);
            permission.Name.AreEqualIgnoringCase(name);
            permission.AreEqual(Processor.Execute(new GetPermission
            {
                EntityID = permission.EntityID
            }).Result);

            code = code.Reverse();
            name = name.Reverse();
            var upd_permission = Processor.Execute(new UpdatePermission
            {
                EntityID = permission.EntityID,
                Code = code,
                Name = name,
                Flags = PermissionFlags.Active
            }).Result;

            Assert.NotNull(upd_permission);
            upd_permission.AreEqual(permission);
            upd_permission.Code.AreEqualIgnoringCase(code);
            upd_permission.Name.AreEqualIgnoringCase(name);
            upd_permission.Flags.AreEqual(PermissionFlags.Active);

            Processor.Execute(new DeletePermission
            {
                EntityID = permission.EntityID
            });
        }

        [Test]
        public void Cannot_Create_If_Code_Empty()
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new CreatePermission
            {
                Code = String.Empty
            }));
        }

        [Test]
        public void Cannot_Update_If_Code_Empty()
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new UpdatePermission
            {
                Code = String.Empty
            }));
        }
    }
}
